/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.match.components;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.MainFrame;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.SpriteSheetCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.AvatarFrameKey;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.PawnSheetKey;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

/**
 * @author Massimo Canonaco
 *
 */
public class EndMatchDialog extends JDialog 
{

	/** */
	private Image backgroundImage;
	
	/** */
	private JLabel accountName;
	
	/** */
	private JLabel matchStatus;
	
	/** */
	private JPanel characterList;
	
	/** */
	private SpriteSheetCache imgCache;
	
	/** */
	private Font font;
	
	/** */
	private Color color;
	
	public EndMatchDialog(final Frame frame, Map<String, String> settings, SpriteSheetCache cache)
	{
		super(frame);

		try {
			OverallGUILayoutFactory gui = OverallGUILayoutFactory.getInstance();
			backgroundImage = ImageIO.read(new FileInputStream(settings.get("endmatchpanel.background.img.path")));

			imgCache = cache;

			GridBagConstraints constraints = new GridBagConstraints();
//			setLayout(new GridBagLayout());
			
			JPanel panel = new JPanel()
			{
				protected void paintComponent(Graphics g) {
					super.paintComponent(g);
					g.drawImage(backgroundImage, 0, 0, null);
				};
			};
			panel.setBackground(gui.getParsedColor(settings.get("endmatchpanel.background.color")));
			panel.setLayout(new GridBagLayout());
			constraints.gridx = 0;
			constraints.gridy = GridBagConstraints.RELATIVE;
			constraints.fill = GridBagConstraints.BOTH;
			constraints.anchor = GridBagConstraints.PAGE_START;
//			constraints.anchor = GridBagConstraints.CENTER;
			constraints.weighty = 0.2f;
			constraints.insets = new Insets(0, 0, 2, 0);

			accountName = new JLabel();
			accountName.setFont(gui.getHeaderFont().deriveFont(Float.parseFloat(settings.get("endmatchpanel.accountname.size"))));
			accountName.setForeground(gui.getParsedColor(settings.get("endmatchpanel.accountname.color")));
			
			accountName.setOpaque(false);
			panel.add(accountName,constraints);

			constraints.weighty = 0.1f;
			matchStatus = new JLabel();
			matchStatus.setFont(gui.getHeaderFont().deriveFont(Float.parseFloat(settings.get("endmatchpanel.matchstatus.size"))));
			matchStatus.setForeground(gui.getParsedColor(settings.get("endmatchpanel.matchstatus.color")));
			matchStatus.setOpaque(false);
			panel.add(matchStatus,constraints);

			constraints.gridheight = 3;
			constraints.weighty = 1.0f;
			characterList = new JPanel();
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.add(characterList);
			characterList.setLayout(new BoxLayout(characterList, BoxLayout.Y_AXIS));
			characterList.setOpaque(false);
			scrollPane.setOpaque(false);
			panel.add(characterList, constraints);
			
			final JDialog dialog = this;
			JButton continueButton = gui.createButtonUniState(Float.parseFloat(settings.get("endmatchpanel.continuebutton.text.size")), "Continua", Integer.parseInt(settings.get("endmatchpanel.continuebutton.size.width")), Integer.parseInt(settings.get("endmatchpanel.continuebutton.size.height")), OverallGUILayoutFactory.BUTTON_STATE_ACTIVE);
			continueButton.addActionListener(new ActionListener() 
			{
				@Override
				public void actionPerformed(ActionEvent arg0) 
				{
					if(frame instanceof MainFrame)
					{
						dialog.setVisible(false);
						dialog.dispose();
						((MainFrame)frame).getGameManager().getClientBattlePlayer().disband();
						((MainFrame)frame).getGameManager().getMatchManager().disband();
						((MainFrame)frame).getGameManagerMenu().showGM(false);
						System.gc();
					}
				}
			});
			continueButton.setOpaque(false);
			continueButton.setBackground(new Color(0, 0, 0, 0));
//			continueButton.setSize(continueButton.getPreferredSize());
//			continueButton.setMaximumSize(continueButton.getPreferredSize());
			constraints.weighty = 0.1f;
			constraints.gridheight = 1;
			panel.add(continueButton,constraints);
			
			add(panel);
			Dimension dimension = new Dimension(backgroundImage.getWidth(null), backgroundImage.getHeight(null));
			setUndecorated(true);
			setMinimumSize(dimension);
			setMaximumSize(dimension);
			setLocation((frame.getWidth()-dimension.width)/2, (frame.getHeight()-dimension.height)/2);
			
			font = gui.getParagraphFont().deriveFont(Float.parseFloat(settings.get("endmatchpanel.characterlist.size")));
			color = gui.getParsedColor(settings.get("endmatchpanel.characterlist.color"));
//			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void set(boolean win, String accountName, List<Character> fighters) throws InvalidFileException
	{
		if(win)
		{
			matchStatus.setText(" Hai vinto! ");
		}
		else
		{
			matchStatus.setText(" Hai perso! ");
		}
		
		matchStatus.setHorizontalAlignment(SwingConstants.CENTER);
		this.accountName.setText(" "+accountName+" ");
		this.accountName.setHorizontalAlignment(SwingConstants.CENTER);
		PawnSheetKey key = new PawnSheetKey();
		AvatarFrameKey avatarFrameKey = new AvatarFrameKey();
		avatarFrameKey.setKey(AvatarFrameKey.MINI_AVATAR);
		key.setFrameKey(avatarFrameKey);
		key.setWhat(PawnSheetKey.FRAME_TYPE_AVATAR);
		key.setWhen("");
		
//		float size = Float.parseFloat()
		for (Character character : fighters) 
		{
			JPanel characterRow = new JPanel();
			characterRow.setOpaque(false);
			key.setWho(character.getType());
			JLabel icon = new JLabel();
			icon.setIcon(new ImageIcon(imgCache.getImage(key, 0, true)));
			icon.setOpaque(false);
			characterRow.add(icon);
			
			JLabel characterNameLevel = new JLabel();
			characterNameLevel.setText(character.getName());
			characterNameLevel.setFont(font);
			characterNameLevel.setForeground(color);
			characterNameLevel.setOpaque(false);
			characterRow.add(characterNameLevel);
			
			characterList.add(characterRow);
		}
	}
}
